package com.ushio.capsuleservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ushio.capsuleservice.mapper.ArchiveMapper;
import com.ushio.capsuleservice.mapper.ArchiveNoteMapper;
import com.ushio.capsuleservice.model.entity.Archive;
import com.ushio.capsuleservice.model.entity.ArchiveNote;
import com.ushio.capsuleservice.service.ArchiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 思维存档服务实现类
 */
@Service
public class ArchiveServiceImpl extends ServiceImpl<ArchiveMapper, Archive> implements ArchiveService {
    
    @Autowired
    private ArchiveNoteMapper archiveNoteMapper;

    @Override
    public List<Archive> findByUserId(Long userId) {
        return lambdaQuery()
                .eq(Archive::getUserId, userId)
                .list();
    }

    @Override
    public boolean deleteByUserId(Long userId) {
        return lambdaUpdate()
                .eq(Archive::getUserId, userId)
                .remove();
    }

    @Override
    public Archive createArchive(Archive archive) {
        archive.setStatus(1);
        archive.setCreatedTime(LocalDateTime.now());
        archive.setUpdatedTime(LocalDateTime.now());
        
        save(archive);
        return archive;
    }
    
    @Override
    public IPage<Archive> getUserArchives(Long userId, int page, int size, String keyword) {
        Page<Archive> pageParam = new Page<>(page, size);
        QueryWrapper<Archive> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("status", 1);
        if (keyword != null && !keyword.trim().isEmpty()) {
            queryWrapper.and(wrapper -> wrapper
                .like("title", keyword)
                .or()
                .like("content", keyword)
                .or()
                .like("tags", keyword)
            );
        }
        queryWrapper.orderByDesc("created_time");
        
        return page(pageParam, queryWrapper);
    }
    
    @Override
    public Archive getArchiveDetail(Long archiveId) {
        Archive archive = getById(archiveId);
        if (archive != null && archive.getStatus() == 1) {
            return archive;
        }
        return null;
    }
    
    @Override
    public Archive updateArchive(Long archiveId, Archive archive) {
        Archive existingArchive = getById(archiveId);
        if (existingArchive == null) {
            throw new RuntimeException("思维存档不存在");
        }
        
        existingArchive.setTitle(archive.getTitle());
        existingArchive.setContent(archive.getContent());
        existingArchive.setTags(archive.getTags());
        existingArchive.setUpdatedTime(LocalDateTime.now());
        
        updateById(existingArchive);
        return existingArchive;
    }
    
    @Override
    public boolean deleteArchive(Long archiveId) {
        Archive archive = getById(archiveId);
        if (archive == null) {
            throw new RuntimeException("思维存档不存在");
        }
        
        archive.setStatus(0);
        archive.setUpdatedTime(LocalDateTime.now());
        
        return updateById(archive);
    }
    
    @Override
    public ArchiveNote addArchiveNote(Long archiveId, String content, Long userId) {
        ArchiveNote note = new ArchiveNote();
        note.setArchiveId(archiveId);
        note.setUserId(userId);
        note.setContent(content);
        note.setCreatedTime(LocalDateTime.now());
        note.setUpdatedTime(LocalDateTime.now());
        
        archiveNoteMapper.insert(note);
        return note;
    }
    
    @Override
    public IPage<ArchiveNote> getArchiveNotes(Long archiveId, int page, int size) {
        Page<ArchiveNote> pageParam = new Page<>(page, size);
        QueryWrapper<ArchiveNote> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.eq("archive_id", archiveId);
        queryWrapper.orderByDesc("created_time");
        
        return archiveNoteMapper.selectPage(pageParam, queryWrapper);
    }
    
    @Override
    public boolean deleteArchiveNote(Long noteId, Long userId) {
        ArchiveNote note = archiveNoteMapper.selectById(noteId);
        if (note == null) {
            throw new RuntimeException("备注不存在");
        }
        
        if (!note.getUserId().equals(userId)) {
            throw new RuntimeException("无权限删除此备注");
        }
        
        return archiveNoteMapper.deleteById(noteId) > 0;
    }
}
